package com.zzptc.fragmenttest1;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.graphics.Color;
import android.icu.text.UnicodeSetSpanner;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.zzptc.fragmenttest1.Bean.User;
import com.zzptc.fragmenttest1.fragment.FirstFragment;
import com.zzptc.fragmenttest1.fragment.SecondFragment;
import com.zzptc.fragmenttest1.fragment.ThirdFragment;

import org.litepal.LitePal;

import java.util.List;

public class MainActivity extends AppCompatActivity {
    private Button tofirst,tosecond,tothird;
    private FirstFragment firstFragment=new FirstFragment();
    private SecondFragment secondFragment=new SecondFragment();
    private ThirdFragment thirdFragment=new ThirdFragment();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        replaceFragment(firstFragment);
      getUser();
      initView();
      setListener();

    }

    private void getUser() {
        List<User> userList= LitePal.findAll(User.class);
        if (userList.size()==0) {
            User user = new User(0, "张三", "123");
            User user1 = new User(0, "李四", "456");
            User user2 = new User(0, "王五", "789");
            User user3 = new User(0, "sw", "111213");
            user.save();
            user1.save();
            user2.save();
            user3.save();
        }

    }


    private void setListener() {
        //按键监听
        View.OnClickListener listener=new View.OnClickListener() {
            @Override
                public void onClick(View v) {
                    switch (v.getId()) {
                        case R.id.tofirst:
                            initColor(v);
                            tofirst.setBackgroundColor(Color.parseColor("#BBFFFF"));//tofirst=v
                            replaceFragment(firstFragment);
                            break;

                        case R.id.tosecond:
                            initColor(v);
                            tosecond.setBackgroundColor(Color.parseColor("purple"));//tofirst=v
                            replaceFragment(secondFragment);
                            break;
                        case R.id.tothird:
                            initColor(v);
                            tothird.setBackgroundColor(Color.parseColor("#48d1cc"));//tofirst=v
                            replaceFragment(thirdFragment);
                            break;
                    }
                }
            };

        tofirst.setOnClickListener(listener);
        tosecond.setOnClickListener(listener);
        tothird.setOnClickListener(listener);
    }

    private void initView() {

        tofirst=findViewById(R.id.tofirst);
        tosecond=findViewById(R.id.tosecond);
        tothird=findViewById(R.id.tothird);
        initColor(tofirst);

    }

    private void initColor(View v){
        tofirst.setBackgroundColor(Color.GRAY);
        tosecond.setBackgroundColor(Color.GRAY);
        tothird.setBackgroundColor(Color.GRAY);
        v.setBackgroundColor(Color.parseColor("#48d1cc"));


    }









    private void replaceFragment(Fragment fragment) {
        FragmentManager fragmentManager=getSupportFragmentManager();
        FragmentTransaction transaction=fragmentManager.beginTransaction();//开启事务

        transaction.replace(R.id.fragment_container,fragment);
        transaction.commit();
    }

}


